package com.decent.ejfadmin.user.controller;

import cn.decent.component.shiro.service.AdminService;
import com.decent.ejfadmin.common.bean.MessageBean;
import com.decent.ejfadmin.common.bean.SupCertificateDto;
import com.decent.ejfadmin.common.bean.SupUser;
import com.decent.ejfadmin.common.exception.OperateFailException;
import com.decent.ejfadmin.user.bean.Page;
import com.decent.ejfadmin.user.service.UserCertificateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * describe:
 *
 * @author JunLei He
 * @date 2018年10月13日
 */
@Slf4j
@RestController
@RequestMapping("/UserCertificateController/")
public class UserCertificateController {
    @Autowired
    private UserCertificateService userCertificateService;
    @Autowired
    private AdminService adminService;

    /**
     * 查询会员实名认证信息列表
     *
     * @param supUser           会员信息
     * @param page              分页
     * @param supCertificateDto 实名认证信息
     * @return 返回列表
     */
    @RequestMapping("queryUserCertificateList")
    public Page<SupCertificateDto> queryUserCertificateList(SupUser supUser, Page page, SupCertificateDto supCertificateDto) {
        try {
            return userCertificateService.queryUserCertificateList(supUser, page, supCertificateDto);
        } catch (Exception e) {
            log.warn(" 查询会员认证信息异常 ", e);
            page.setList(new ArrayList<>());
            page.setTotal(0);
            return page;
        }
    }

    /**
     * 查询图片
     *
     * @param id 支付宝id
     * @return 返回图片
     */
    @RequestMapping("queryLevelEsignPic")
    public List<String> queryLevelEsignPic(Long id) {
        try {
            return userCertificateService.queryLevelEsignPic(id);
        } catch (Exception e) {
            log.warn(" 查询会员认证信息异常 {}", e, e);
            return new ArrayList<>();
        }
    }

    /**
     * 修改会员实名认证状态
     *
     * @param id     实名认证id
     * @param status 实名认证状态
     * @param remark 备注
     * @return 返回
     */
    @RequestMapping("updateUserCertificate")
    public MessageBean updateUserCertificate(Long id, Integer status, String remark) {
        try {
            if (Objects.isNull(id) || Objects.isNull(status)) {
                return MessageBean.fail("操作失败,请求刷新页面重试");
            }
            return userCertificateService.updateUserCertificate(id, status, remark);
        } catch (OperateFailException e) {
            log.warn("认证id:[{}]|修改状态[{}]查询会员认证信息异常", id, status, e);
            return MessageBean.fail(e.getMessage());
        } catch (Exception e) {
            log.warn("认证id:[{}]|修改状态[{}]查询会员认证信息错误", id, status, e);
            return MessageBean.fail("修改会员实名认证信息异常");
        }
    }

    /**
     * 添加实名认证备注
     *
     * @param id     实名认证id
     * @param remark 备注
     * @return 返回
     */
    @RequestMapping("addRemark")
    public MessageBean addRemark(Long id, String remark) {
        try {
            if (Objects.isNull(id) || StringUtils.isBlank(remark)) {
                return MessageBean.fail("请输入备注");
            }
            return userCertificateService.addRemark(id, remark);
        } catch (Exception e) {
            log.warn("添加实名认证备注错误[{}],[{}],[{}]", id, remark, e, e);
            return MessageBean.fail("添加实名认证备注异常");
        }
    }

}
